/*
 * DominionBuyIntegration.c
 *
 *  Created on: Mar 19, 2014
 *      Author: Laurye_OSU
 */


#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include "rngs.h"
#include "interface.h"

int numerrors=0;

int randInt(int low, int high)
{
	int temp = low + rand() % (high -low +1);
	return temp;

}



//structure borrowed from Alex Carpenter
void testBuyPhase(int money, int *kingdomCards, struct gameState* state) {

	int randCard;
	int playCard_error;
	int count;
	count = 0;

	while (state->numBuys > 0) {
		//make sure when I play a card I don't get an error
		playCard_error = -1;

		if (money >= 8) {
			playCard_error = buyCard(province, state);
			if (playCard_error != 0) { numerrors++; }
		}
		else if (money == 6) {
			randCard = floor(Random() * 2);
			if (randCard == 0) {
				playCard_error = buyCard(gold, state);
				if (playCard_error != 0) { numerrors++; }
			}
			else if (randCard == 1) {
				playCard_error = buyCard(adventurer, state);
				if (playCard_error != 0) {numerrors++;}
			}
		}
		else if (money == 5) {
			randCard = floor(Random() * 7);
			if (randCard == 0) {
				playCard_error = buyCard(duchy, state);
				if (playCard_error != 0) {numerrors++;}
			}
			if (randCard == 1) {
				playCard_error = buyCard(silver, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 2) {
				playCard_error = buyCard(council_room, state);
				if (playCard_error != 0) {numerrors++;}

			}
			else if (randCard == 3) {
				playCard_error = buyCard(mine, state);
				if (playCard_error != 0) {numerrors++;}

			}
			else if (randCard == 4) {
				playCard_error = buyCard(minion, state);
				if (playCard_error != 0) {numerrors++;}
			} else if (randCard == 5) {
				playCard_error = buyCard(tribute, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 6) {
				playCard_error = buyCard(outpost, state);
				if (playCard_error != 0) {numerrors++;	}
			}
		}
		else if (money == 4) {
			randCard = floor(Random() * 10);
			if (randCard == 0) {
				playCard_error = buyCard(silver, state);
				if (playCard_error != 0) {numerrors++;}
			}
			if (randCard == 1) {
				playCard_error = buyCard(feast, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 2) {
				playCard_error = buyCard(gardens, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 3) {
				playCard_error = buyCard(remodel, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 4) {
				playCard_error = buyCard(smithy, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 5) {
				playCard_error = buyCard(baron, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 6) {
				playCard_error = buyCard(cutpurse, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 7) {
				playCard_error = buyCard(salvager, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 8) {
				playCard_error = buyCard(sea_hag, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 9) {
				playCard_error = buyCard(treasure_map, state);
				if (playCard_error != 0) {numerrors++;}
			}
		}
		else if (money == 3) {
			randCard = floor(Random() * 5);
			if (randCard == 0) {
				playCard_error = buyCard(silver, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 1) {
				playCard_error = buyCard(village, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 2) {
				playCard_error = buyCard(great_hall, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 3) {
				playCard_error = buyCard(steward, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 4) {
				playCard_error = buyCard(ambassador, state);
				if (playCard_error != 0) {numerrors++;}
			}
		}
		else if (money == 2) {
			randCard = floor(Random() * 3);
			if (randCard == 0) {
				playCard_error = buyCard(estate, state);
				if (playCard_error != 0) {numerrors++;}
			}
			else if (randCard == 1) {
				playCard_error = buyCard(copper, state);
				if (playCard_error == 1) {numerrors++;}
			}
			else if (randCard == 2) {
				playCard_error = buyCard(embargo, state);
				if (playCard_error == 1) {numerrors++;}
			}
		}
		else if (money >= 0) {
			randCard = floor(Random() * 2);
			if (randCard == 0) {
				playCard_error = buyCard(copper, state);
				if (playCard_error != 0) {numerrors++;}
			} else if (randCard == 1) {
				playCard_error = buyCard(curse, state);
				if (playCard_error != 0) {numerrors++;}
			}
		}

		count++;

		if (count > 50 || playCard_error != 0) {
			if (count > 50) {
				//fprintf("Did not buy anything\n");
				state->numBuys--;
			}
		count = 0;
		}

	}

}



void playGame(int seed){

	int n;
	int i;
	int numPlayers;
	int player;
	int success;
	int k[10];
	int money = 0;

	//set up how many random games to play
	for (n = 0; n < 100; n++) {

	newGame();
	struct gameState G;
	numPlayers = floor(Random() * MAX_PLAYERS); //make number of players random
	  if (numPlayers < 2){ 	numPlayers = 2;  }
	  else if (numPlayers > 4){	numPlayers= 4;  }

	  //generate random kingdom cards
	  selectKingdomCards(rand(),k);


	//Initialize Game
	success = initializeGame(numPlayers, k, seed, &G);

	if (success != 0) {
		printf("Initializing Game Error");
		numerrors++;
		exit(0);
	}


    	/*Give each player a turn*/
    	for(player = 0;player < numPlayers;player++){


    		money = 0;
    		i=0;

    		//count coins in hand
    		int money = countHandCoins(player, &G);


    	    //Buy Phase
    	    G.phase = 1;
    	    testBuyPhase(money, k, &G);


    		} //end for each player




	} // for loop for how many games

}


int main (int argc, char** argv)
{


	int seed;


	seed = rand();
	SelectStream(1);
	PutSeed(seed);

	playGame(seed);
	printf("All buy phase tests ran successfully\n");
	printf("Number of errors found was %d\n", numerrors);


	return 0;
}



